package com.linewow.xhyy.nihaonew.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.activity.TopicDetailActivity;
import com.linewow.xhyy.nihaonew.activity.UserMineActivity;
import com.linewow.xhyy.nihaonew.entity.topic.FindTopicInfo;
import com.linewow.xhyy.nihaonew.entity.image.ImagesInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.event.MessageEvent;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.squareup.picasso.Picasso;

import org.simple.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by LXR on 2016/8/29.
 * 3图列表
 */
public class FindTopicAdapter extends BaseAdapter {
    private List<FindTopicInfo<ImagesInfo, Uinfo>> list;
    private Context context;
    private String TAG="FindTopicAdapter";

    public FindTopicAdapter(List<FindTopicInfo<ImagesInfo, Uinfo>> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_find, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        FindTopicInfo<ImagesInfo, Uinfo> findTopicInfo = list.get(position);
        final List<ImagesInfo> imagesInfoList = findTopicInfo.images;
        final Uinfo uinfo = findTopicInfo.uinfo;
        viewHolder.itemFindNickname.setText(uinfo.nickname);
        viewHolder.itemFindAttentionNum.setText(uinfo.fans);

        if (uinfo.gender.equals("1")) {
            viewHolder.itemFindGender.setVisibility(View.VISIBLE);
            viewHolder.itemFindGender.setImageResource(R.mipmap.icon_gender_male);
        } else if (uinfo.gender.equals("2")) {
            viewHolder.itemFindGender.setVisibility(View.VISIBLE);
            viewHolder.itemFindGender.setImageResource(R.mipmap.icon_gender_female);
        } else {
            viewHolder.itemFindGender.setVisibility(View.INVISIBLE);
        }
        if(findTopicInfo.area!=null&&!findTopicInfo.area.equals("null")){
            viewHolder.itemFindCountry.setText(findTopicInfo.area);
        }else{
            viewHolder.itemFindCountry.setText(context.getResources().getString(R.string.message_no_area));
        }
        if(findTopicInfo.isfollow){
            viewHolder.itemFindAttentionTv.setText(context.getResources().getString(R.string.widget_follow_done));
        }else{
            viewHolder.itemFindAttentionTv.setText(context.getResources().getString(R.string.widget_attention));
        }
        Picasso.with(context).load((HttpModel.HOMEURL+findTopicInfo.avatar).replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_avatar).into(viewHolder.itemFindAvatar);
        int size=imagesInfoList.size();
        viewHolder.itemFindImg3.setVisibility(View.INVISIBLE);
        viewHolder.itemFindImg2.setVisibility(View.INVISIBLE);
        viewHolder.itemFindImg1.setVisibility(View.INVISIBLE);
        if(size>0){
            viewHolder.itemFindImg1.setVisibility(View.VISIBLE);
            Picasso.with(context).load(imagesInfoList.get(0).path.replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_topic).into(viewHolder.itemFindImg1);
            viewHolder.itemFindImg1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG,"点击的tid1>>>"+imagesInfoList.get(0).tid);
                    String tid=imagesInfoList.get(0).tid;
                    startDetailActivity(tid);
                }
            });

        }
        if(size>1){
            viewHolder.itemFindImg2.setVisibility(View.VISIBLE);
            Picasso.with(context).load(imagesInfoList.get(1).path.replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_topic).into(viewHolder.itemFindImg2);
            viewHolder.itemFindImg2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG,"点击的tid2>>>"+imagesInfoList.get(1).tid);
                    String tid=imagesInfoList.get(1).tid;
                    startDetailActivity(tid);
                }
            });
        }
        if(size>2){
            viewHolder.itemFindImg3.setVisibility(View.VISIBLE);
            Picasso.with(context).load(imagesInfoList.get(2).path.replace("m.en1on1.com", "huibenabc.com")).fit().error(R.mipmap.err_topic).into(viewHolder.itemFindImg3);
            viewHolder.itemFindImg3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG,"点击的tid3>>>"+imagesInfoList.get(2).tid);
                    String tid=imagesInfoList.get(2).tid;
                    startDetailActivity(tid);
                }
            });
        }

        viewHolder.itemFindAttentionRela.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageEvent messageEvent=new MessageEvent();
                messageEvent.setIndex(position);
                EventBus.getDefault().post(messageEvent,TAG);//发送给fragment处理
            }
        });
        viewHolder.itemFindAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startUserActivity(uinfo);
            }
        });
        return convertView;
    }

    public void startDetailActivity(String tid){
        Intent intent=new Intent(context, TopicDetailActivity.class);
        intent.putExtra("msg",tid);
        context.startActivity(intent);
    }

    public void updateView(View view ,int index){
        if(view==null){
            return;
        }
        ViewHolder viewHolder= (ViewHolder) view.getTag();
        FindTopicInfo<ImagesInfo, Uinfo>findTopicInfo=list.get(index);
        if(findTopicInfo.isfollow){
            viewHolder.itemFindAttentionTv.setText(context.getResources().getString(R.string.widget_follow_cancel));
        }else{
            viewHolder.itemFindAttentionTv.setText(context.getResources().getString(R.string.widget_attention));
        }
    }

    static class ViewHolder {
        @Bind(R.id.item_find_avatar)
        ImageView itemFindAvatar;
        @Bind(R.id.item_find_gender)
        ImageView itemFindGender;
        @Bind(R.id.item_find_nickname)
        TextView itemFindNickname;
        @Bind(R.id.item_find_attention_num)
        TextView itemFindAttentionNum;
        @Bind(R.id.item_find_attention_tv)
        TextView itemFindAttentionTv;
        @Bind(R.id.item_find_attention_rela)
        RelativeLayout itemFindAttentionRela;
        @Bind(R.id.item_find_img1)
        ImageView itemFindImg1;
        @Bind(R.id.item_find_img2)
        ImageView itemFindImg2;
        @Bind(R.id.item_find_img3)
        ImageView itemFindImg3;
        @Bind(R.id.item_find_country)
        TextView itemFindCountry;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private void startUserActivity(Uinfo info) {
        Intent intent=new Intent(context, UserMineActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelable("info",info);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
}
